package com.fesine.lock.thread;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @description: 类描述
 * @author: Fesine
 * @createTime:2017/4/12 10:55
 * @update:修改内容
 * @author: Fesine
 * @updateTime:2017/4/12 10:55
 */
public class SellTicketsThread implements Runnable {

    private int tickets = 100;

    private Lock lock = new ReentrantLock();

    @Override
    public void run() {
        while (true) {
            try {
                lock.lock();
                if (tickets > 0) {
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + ":正在售出第" + (tickets--)
                            + "张票");
                }
            } finally {
                lock.unlock();
            }
        }
    }
}
